package com.dime.volumnwidget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.RemoteViews;

public class VolumnWidgetProvider extends AppWidgetProvider {
    //音量+
    String ACTION_VOLUMN_UP = "com.dime.volumnwidget.action.volumn_up";
    //音量-
    String ACTION_VOLUMN_DOWN = "com.dime.volumnwidget.action.volumn_down";
    //静音
    String ACTION_VOLUMN_MUTE = "com.dime.volumnwidget.action.volumn_mute";

    VolumnUtils volumnUtils;
    RemoteViews remoteViews;
    ComponentName componentName;

    private RemoteViews initView(Context context){
        if (remoteViews == null) {
            remoteViews = new RemoteViews(context.getPackageName(), R.layout.volumn_widget_layout);
        }
        if (volumnUtils == null) {
            volumnUtils = new VolumnUtils(context);
        }

        //设置标题, 没什么必要
        remoteViews.setTextViewText(R.id.tv_app_widget_title, "音量调节:");


        //设置音量数字点击事件
        Intent systemSoundSettingIntent = new Intent(context, VolumnWidgetProvider.class).setAction(Settings.ACTION_SOUND_SETTINGS);
        PendingIntent sssPi = PendingIntent.getActivity(context, 200, systemSoundSettingIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_CANCEL_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.tv_volumn_number, sssPi);

        //设置当前音量显示
        remoteViews.setTextViewText(R.id.tv_volumn_number, volumnUtils.getCurrentVolumn() + "");

        //设置点击事件, 跳转到主界面
        Intent mainActivityIntent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 300, mainActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_CANCEL_CURRENT);

        remoteViews.setOnClickPendingIntent(R.id.tv_app_widget_title, pendingIntent);

        //设置音量按钮点击事件 +
        Intent upIntent = new Intent(context, VolumnWidgetProvider.class)
                .addCategory(Intent.CATEGORY_ALTERNATIVE)
                .setAction(ACTION_VOLUMN_UP);
        PendingIntent volumnUpPIntent = PendingIntent.getBroadcast(context, 400, upIntent,
                android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S ? (PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE) : 0);
        remoteViews.setOnClickPendingIntent(R.id.btn_up, volumnUpPIntent);

        //设置音量按钮点击事件 -
        Intent downIntent = new Intent(context, VolumnWidgetProvider.class)
                .addCategory(Intent.CATEGORY_ALTERNATIVE)
                .setAction(ACTION_VOLUMN_DOWN);
        PendingIntent volumnDownPIntent = PendingIntent.getBroadcast(context, 500, downIntent,
                android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S ? (PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE) : 0);
        remoteViews.setOnClickPendingIntent(R.id.btn_down, volumnDownPIntent);

        //设置音量按钮点击事件 mute
        Intent muteIntent = new Intent(context, VolumnWidgetProvider.class)
                .addCategory(Intent.CATEGORY_ALTERNATIVE)
                .setAction(ACTION_VOLUMN_MUTE);
        PendingIntent mutePIntent = PendingIntent.getBroadcast(context, 600, muteIntent,
                android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S ? (PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE) : 0);
        remoteViews.setOnClickPendingIntent(R.id.btn_mute, mutePIntent);

        return remoteViews;
//        componentName = new ComponentName(context, VolumnWidgetProvider.class);
//        appWidgetManager.updateAppWidget(componentName, remoteViews);
    }


    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        Log.e("dime_log", "onUpdate");

        remoteViews = initView(context);
        componentName = new ComponentName(context, VolumnWidgetProvider.class);
        appWidgetManager.updateAppWidget(componentName, remoteViews);
    }

    /**
     * 用户第一次添加小组件, 添加第二个不会出发
     *
     * @param context
     */
    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        Log.e("dime_log", "onEnabled");
    }

    /**
     * 正好想法，最后一次删除小组件
     *
     * @param context
     */
    @Override
    public void onDisabled(Context context) {
        super.onDisabled(context);
        Log.e("dime_log", "onDisabled");
    }

    /**
     * 当 Widget 第一次被添加或者大小发生变化时调用该方法，可以在此控制 Widget 元素的显示和隐藏。
     * 当小部件布局发生更改的时候调用。对应广播的 Action 为 ACTION_APPWIDGET_OPTIONS_CHANGED。
     *
     * @param context
     * @param appWidgetManager 您可以调用 AppWidgetManager.updateAppWidget 的 AppWidgetManager 对象。
     * @param appWidgetId      大小改变的widget的appWidgetId。
     * @param newOptions
     */
    @Override
    public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
        super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
        Log.e("dime_log", "onAppWidgetOptionsChanged");
    }

    /**
     * 当小部件从备份中还原，或者恢复设置的时候，会调用，实际用的比较少。对应广播的 Action 为 ACTION_APPWIDGET_RESTORED。
     *
     * @param context
     * @param oldWidgetIds
     * @param newWidgetIds
     */
    @Override
    public void onRestored(Context context, int[] oldWidgetIds, int[] newWidgetIds) {
        super.onRestored(context, oldWidgetIds, newWidgetIds);
    }

    /**
     * 接收广播的回调函数
     * * onReceive() 中处理的是 Widget 相关的广播事件，然后分发到各个回调函数中onUpdate(), onDeleted(), onEnabled(), onDisabled, onAppWidgetOptionsChanged()。
     *
     * @param context
     * @param intent
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        //volumnUtils， remoteViews, componetName每次必定是kong，此处待优化
        volumnUtils = new VolumnUtils(context);
        remoteViews = new RemoteViews(context.getPackageName(), R.layout.volumn_widget_layout);
        componentName = new ComponentName(context, VolumnWidgetProvider.class);

        String action = intent.getAction();
        Log.e("dime_log", "onReceive action=" + action);

        int currentVolumn = 0;
        if (action.equals(AppWidgetManager.ACTION_APPWIDGET_ENABLED)) {

        } else if (action.equals(ACTION_VOLUMN_UP)) {
            currentVolumn = volumnUtils.volumnUp();
//            remoteViews.setTextViewText(R.id.tv_volumn_number, currentVolumn + "");
//            AppWidgetManager.getInstance(context).updateAppWidget(componentName, remoteViews);
        } else if (action.equals(ACTION_VOLUMN_DOWN)) {
            currentVolumn = volumnUtils.volumnDown();
//            remoteViews.setTextViewText(R.id.tv_volumn_number, currentVolumn + "");
//            AppWidgetManager.getInstance(context).updateAppWidget(componentName, remoteViews);
        } else if (action.equals(ACTION_VOLUMN_MUTE)) {
            currentVolumn = volumnUtils.volumnMute();
//            remoteViews.setTextViewText(R.id.tv_volumn_number, currentVolumn + "");
//            AppWidgetManager.getInstance(context).updateAppWidget(new ComponentName(context, VolumnWidgetProvider.class), remoteViews);
        }
        remoteViews = initView(context);
        componentName = new ComponentName(context, VolumnWidgetProvider.class);
        AppWidgetManager.getInstance(context).updateAppWidget(componentName, remoteViews);

        super.onReceive(context, intent);

    }
}
